﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using VemTransporte.Models.Fachada;
using VemTransporte.Models.Basica;
using VemTransporte.Models.Factory;
using System.Globalization;

namespace VemTransporte
{
    public partial class Default : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            Session["UsuarioLogado"] = null;
            Session["IdLinhaUsuarioLogado"] = null;

            if (!IsPostBack)
            {
                DropDownListDia.Items.Clear();
                DropDownListDia.Items.Add("Dia");
                DropDownListMes.Items.Clear();
                DropDownListMes.Items.Add("Mês");
                DropDownListAno.Items.Clear();
                DropDownListAno.Items.Add("Ano");

                for (int i = 1; i <= 31; i++)
                {
                    DropDownListDia.Items.Add(string.Format("{0:00}", i));
                }
                CultureInfo culture = new CultureInfo("pt-BR");
                DateTimeFormatInfo dtfi = culture.DateTimeFormat;

                for (int i = 1; i <= 12; i++)
                {
                    string mes = culture.TextInfo.ToTitleCase(dtfi.GetMonthName(i));
                    DropDownListMes.Items.Add(mes);
                }

                for (int i = 1910; i <= DateTime.Now.Year - 12; i++)
                {
                    DropDownListAno.Items.Add(Convert.ToString(i));
                }
            }

        }

        protected void CreateUserButton_Click(object sender, EventArgs e)
        {
            try
            {
                Usuario novoUsuario = PessoaFactory.Instance.CreateUsuario();

                novoUsuario.Nome = UserName.Text;
                novoUsuario.Email = Email.Text;
                novoUsuario.Senha = Password.Text;
                novoUsuario.Nascimento = DateTime.Parse(
                    DropDownListDia.Items[DropDownListDia.SelectedIndex].Text + "/" +
                    DropDownListMes.SelectedIndex + "/" +
                        DropDownListAno.Items[DropDownListAno.SelectedIndex].Text);

                //if(novoUsuario.Nascimento.Year)
                Fachada.Instance.Salvar(novoUsuario);

                if (novoUsuario.Id > 0)
                {
                    Session["UsuarioLogado"] = novoUsuario;
                    Session["IdUsuarioLogado"] = novoUsuario.Id;
                    //Redirect para o mapa implementar aqui
                    Response.Redirect("Views/UsuarioTransporte/MenuUsuario.aspx", false);
                }
                else
                {
                    //Usuario não foi cadastrado
                    Response.Redirect("Cadastro.aspx", false);
                }

            }
            catch (Exception ex)
            {
                ErrorMessage.Text = ex.Message;
            }
        }

        protected void LoginButton_Click(object sender, EventArgs e)
        {
            try
            {
                Usuario usuarioLogado = Fachada.Instance.ProcurarUsuarioPorEmailESenha(TextBoxEmail.Text, TextBoxSenha.Text);
                if (usuarioLogado != null)
                {
                    Session["UsuarioLogado"] = usuarioLogado;
                    Session["IdUsuarioLogado"] = usuarioLogado.Id;
                    //Redirect para o mapa implementar aqui
                    Response.Redirect("Views/UsuarioTransporte/MenuUsuario.aspx", false);
                }
                else
                {
                    //Usuario não encontrado exception
                    Response.Redirect("Views/UsuarioTransporte/Login.aspx", false);
                }
            }
            catch(Exception ex)
            {
                Response.Redirect("Views/UsuarioTransporte/Login.aspx?ErrorMessageLogin=" + ex.Message);
            }

        }

        protected void DropDownListMes_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (((DropDownListMes.SelectedIndex) == 4) || ((DropDownListMes.SelectedIndex) == 6) || ((DropDownListMes.SelectedIndex ) == 9)
                || ((DropDownListMes.SelectedIndex) == 11)) {

                    DropDownListDia.Items.Clear();
                    DropDownListDia.Items.Add("Dia");

                    for (int i = 1; i <= 30; i++)
                    {
                        DropDownListDia.Items.Add(string.Format("{0:00}", i));
                    }
                    DropDownListDia.DataBind();
            }
            else if (DropDownListMes.SelectedIndex == 2)
            {
                DropDownListDia.Items.Clear();
                DropDownListDia.Items.Add("Dia");

                for (int i = 1; i <= 28; i++)
                {
                    DropDownListDia.Items.Add(string.Format("{0:00}", i));
                }
                DropDownListDia.DataBind();
            }
            else {
                DropDownListDia.Items.Clear();
                DropDownListDia.Items.Add("Dia");

                for (int i = 1; i <= 31; i++)
                {
                    DropDownListDia.Items.Add(string.Format("{0:00}", i));
                }
                DropDownListDia.DataBind();
            }
        }

    }
}